package spit;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class CardSlot extends JLayeredPane {
	
	//private JLayeredPane layeredPane;
	
	private ArrayList<String> suits;
	private ArrayList<String> ranks; 
	
	private Card testCard;
	private Point origin;
	
	private List cardList;
	
	private SpitPlayer spitPlayer;
	
	public CardSlot()
	{
		origin = new Point(10, 10);
		
		testCard = new Card();
		cardList = new ArrayList();
		
		suits = new ArrayList<>();
		suits.add("Clubs");
		suits.add("Diamonds");
		suits.add("Hearts");
		suits.add("Spades");
		
		ranks = new ArrayList<>();
		ranks.add("Ace");
		ranks.add("2");
		ranks.add("3");
		ranks.add("4");
		ranks.add("5");
		ranks.add("6");
		ranks.add("7");
		ranks.add("8");
		ranks.add("9");
		ranks.add("10");
		ranks.add("Jack");
		ranks.add("Queen");
		ranks.add("King");
		
		//layeredPane = new JLayeredPane();

	    this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	     
	    this.setPreferredSize(new Dimension(testCard.getWidth()+20, testCard.getHeight()+20));

        //this.add(layeredPane);
	}
	
	public void setAsFreshDeck()
	{
	           
	       int depth = 0;
	               
	       Card card = new Card();
	       
	       for (String rank : ranks )
	       {
	    	   for ( String suit : suits)
	    	   {           		
	    		   card = new Card(rank, suit);
 	    		   card.setBounds(origin.x, origin.y, card.getWidth(), card.getHeight());
	    		   this.add(card, depth);
	    		   cardList.add(card);

	    		   depth++;
	       	   }
	       }
	       
		   
	}
	
	public void shuffle()
	{
	
		Component[] componentsInSlot = this.getComponents();
		
		int length = componentsInSlot.length;
	    Random random = new Random();
	    random.nextInt();
	    for (int index = 0; index < length; index++) 
	    {
	      int change = index + random.nextInt(length - index);
     
	      Component helper = componentsInSlot[index];
	      componentsInSlot[index] = componentsInSlot[change];
	      componentsInSlot[change] = helper;
	    }
	    
	    this.removeAll();
	    
	    for ( Component component : componentsInSlot)
	    {
	    	this.add((Card) component );
	    }
	}
	
	public void split(CardSlot aCardSlot, CardSlot anotherCardSlot)
	{
		int slotCount = this.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER);
		int halfPoint = slotCount/2;
		
		Component[] cardList = this.getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
		
		for ( int index = 0; index < halfPoint; index++)
		{
			aCardSlot.add(cardList[index]);			
		}
		for ( int index = halfPoint; index < slotCount; index++)
		{
			anotherCardSlot.add(cardList[index]);
		}
		
		System.out.println(aCardSlot.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER));
		System.out.println(anotherCardSlot.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER));

		System.out.println(this.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER));
	
	
	}
	
	public void deal(CardSlot firstCardSlot, CardSlot secondCardSlot, CardSlot thirdCardSlot, CardSlot fourthCardSlot, CardSlot fifthCardSlot)
	{
		int slotCount = this.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER);
		
		Component[] cardList = this.getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
		
		int slotRotation = 0;
		int slotRotationPeriod = 0;
		
		for ( int index = 0; index < slotCount && index < 15; index++ )
		{
			Random random = new Random();
			
			int offset = random.nextInt(11);
			offset = offset - 5;
			
			if  ( slotRotation == 0)
			{
				Card card = (Card) cardList[index];
	  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
				firstCardSlot.add(card, 0);
				slotRotation++;
			}
			else if ( slotRotation == 1)
			{
				Card card = (Card) cardList[index];
	  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
				secondCardSlot.add(card, 0);
				slotRotation++;
			}
			else if ( slotRotation == 2)
			{
				Card card = (Card) cardList[index];
	  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
				thirdCardSlot.add(card, 0);
				slotRotation++;
			}
			else if ( slotRotation == 3)
			{
				Card card = (Card) cardList[index];
	  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
				fourthCardSlot.add(card, 0);
				slotRotation++;
			}
			else 
			{
				Card card = (Card) cardList[index];
	  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
				fifthCardSlot.add(card, 0);
				slotRotationPeriod++;
				slotRotation = slotRotationPeriod;
			}
					
		}
		
		firstCardSlot.flipTopCard();
		secondCardSlot.flipTopCard();
		thirdCardSlot.flipTopCard();
		fourthCardSlot.flipTopCard();
		fifthCardSlot.flipTopCard();
		
	}
	
	public void spit(CardSlot spitPileSlot)
	{
		Random random = new Random();
		
		int offset = random.nextInt(11);
		offset = offset - 5;
		
		if ( this.getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER) > 0)
		{
			Component[] cardList = this.getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
			Card card = (Card) cardList[0];
  		    card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
			
			spitPileSlot.acceptCard(card);
			
			
			
			spitPileSlot.flipTopCard();
		}
	}

	public void acceptCard(Card aCard)
	{
	
		final Card card = aCard;
		
		Random random = new Random();
		
		int offset = random.nextInt(11);
		offset = offset - 5;
		
		card.setBounds(origin.x+offset, origin.y+offset, card.getWidth(), card.getHeight());
				
   		CardSlot.this.add(card, 0);	

	}
	
	public void flipTopCard()
	{ 
		Card cardToFlip;
		
		Component[] cardList = this.getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
		
		if ( cardList.length > 0)
		{
			cardToFlip = ((Card) cardList[0]);
			cardToFlip.flip();
		}


	}
	
	public Card getTopCard()
	{
		Component[] cardList = this.getComponents();
		//System.out.println((Card) cardList[0]);
		return (Card) cardList[0];

	}
	
	public boolean topCardSameAs(Card aCard)
	{
		Card card = aCard;
		
		if ( this.getTopCard().rankEquals(card))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean topCardRankNextTo(Card aCard)
	{
		Card card = aCard;
		
		if ( this.getTopCard().rankNextTo(card))
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}



}
